<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="orderNo">
              <el-input v-model="filters.orderNo" placeholder="请输入订单编号"></el-input>
            </el-form-item>
            <el-form-item prop="taskType">
              <el-select v-model="filters.taskType" clearable filterable placeholder="任务类型">
                <el-option v-for="item in $Options.taskTypeList" :key="item.id" :value="item.id" :label="item.name"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="taskState">
              <el-select v-model="filters.taskState" clearable filterable placeholder="任务状态">
                <el-option v-for="item in $Options.taskStateList" :key="item.id" :value="item.id" :label="item.name"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div></div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table ref="tableList" :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据！">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>

        <el-table-column label="订单编号" prop="orderNo" align="center"> </el-table-column>
        <el-table-column label="任务类型" align="center">
          <template slot-scope="{ row }">
            <div>
              {{ $Filters.formatTaskType(row.taskType) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="任务状态" align="center">
          <template slot-scope="{ row }">
            <el-tag style="color: white" size="mini" :color="$Filters.formatTaskState(row.taskState).color">{{ $Filters.formatTaskState(row.taskState).name }} </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="服务员" prop="waiterName"></el-table-column>
        <!-- <el-table-column label="状态" align="center" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag
                size="mini"
                effect="dark"
                :type="$Filters.formatStates(scope.row.state)"
              >
                {{ $Filters.formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column> -->
        <el-table-column align="center" label="完成时间" prop="completeTime"></el-table-column>
        <el-table-column align="center" width="200" label="创建时间" prop="createDate"> </el-table-column>
        <el-table-column label="操作" width="136" fixed="right">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
                <el-dropdown-item v-has="permissionCancelTask" @click.native="handleCancelTask(scope.row)"> 取消任务 </el-dropdown-item>
                <el-dropdown-item v-has="permissionTimeout" @click.native="handleTimeout(scope.row)"> 设置超时时间 </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>

    <!-- 修改任务状态 -->
    <el-dialog class="custom-size__dialog" width="fit-content" center :visible.sync="taskState.show">
      <el-select style="width: 200px" v-model="taskState.taskState" placeholder="请选择任务状态">
        <el-option v-for="item in $Options.taskStateList" :value="item.id" :label="item.name" :key="item.id"></el-option>
      </el-select>
      <div slot="footer">
        <el-button type="primary" @click="">确定</el-button>
        <el-button @click="taskState.show = false">取消</el-button>
      </div>
    </el-dialog>
    <!-- 修改超时时间 -->
    <el-dialog :visible.sync="taskTimeout.show" width="fit-content" title="设置超时时间" center :close-on-click-modal="false">
      <div class="left-right__layout">
        <div class="left-right__item">
          <div class="label" style="width: 160px">单次超时未结单时间</div>
          <el-select class="value time-select" v-model="taskTimeout.taskSingleTimeout" allow-create clearable filterable>
            <el-option v-for="item in timeList" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </div>
        <div class="left-right__item">
          <div class="label" style="width: 160px">总次超时未结单时间</div>
          <el-select class="value time-select" v-model="taskTimeout.taskTotalTimeout" allow-create clearable filterable>
            <el-option v-for="item in timeList" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </div>
      </div>

      <div slot="footer">
        <el-button type="primary" @click="confirmSetTimeout">确定</el-button>
        <el-button @click="taskTimeout.show = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import TaskApi from '@/api/business/task';
import OrderApi from '@/api/order/order';
import * as message from '@/constants/message';
import { getStorage } from '@/utils/auth';
import { USER_INFO } from '@/constants/storeName';

export default {
  name: 'TaskManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      listLoading: true,
      filters: {
        taskType: '',
        taskState: '',
        orderNo: '',
      },
      businessList: [],
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      taskState: {
        show: false,
        taskState: '',
      },
      taskTimeout: {
        show: false,
        taskSingleTimeout: '',
        taskTotalTimeout: '',
      },
      timeList: [
        { id: 10, name: '10' },
        { id: 30, name: '30' },
        { id: 60, name: '60' },
      ],

      permissionAdd: 'SYSTEM_MANAGER_BUSINESS_TASK_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_BUSINESS_TASK_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_BUSINESS_TASK_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_BUSINESS_TASK_LIST_DELETE',
      permissionCancelTask: 'SYSTEM_MANAGER_BUSINESS_TASK_LIST_CANCEL_TASK',
      permissionTimeout: 'SYSTEM_MANAGER_BUSINESS_TASK_LIST_TIMEOUT',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  watch: {
    //to去哪，from从哪来
    // $route(to,from){
    //   if(from.path =='/equip/specimenEdit' || from.path=='/equip/specimenAdd'){
    //       this.listData();
    //   }
    // }
  },
  methods: {
    init() {
      // 获取用户的角色信息
      this.listLoading = true;
      this.listData();
      this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },
    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    // async handleTaskState(row) {
    //   const { taskState } = this;
    //   const { data } = await TaskApi.AAA({ id: row.taskId });
    //   taskState.show = true;
    //   taskState.taskState = data || "";
    //   this.$Utils.notifySuccess("设置成功");
    //   this.listData();
    // },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listData();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listData();
    },
    handleClickDetail(row) {
      this.$router.push({
        path: '/order/orderDetail',
        query: { id: row.orderId },
      });
    },
    // 处理超时
    handleTimeout(row) {
      const { taskTimeout } = this;
      this.rowItem = row;
      taskTimeout.taskSingleTimeout = row.taskSingleTimeout;
      taskTimeout.taskTotalTimeout = row.taskTotalTimeout;
      taskTimeout.show = true;
    },
    async confirmSetTimeout() {
      const {
        taskTimeout,
        rowItem: { taskId },
      } = this;
      const params = {
        taskId,
        taskSingleTimeout: Number(taskTimeout.taskSingleTimeout),
        taskTotalTimeout: Number(taskTimeout.taskTotalTimeout),
      };
      await TaskApi.setTaskTimeout(params);
      taskTimeout.show = false;
      this.$Utils.notifySuccess('设置成功');
      this.listData();
    },
    async handleCancelTask({ orderId }) {
      // const {taskState} = this
      // await TaskApi.modifyTaskState({taskId,state:taskState.taskState})
      await this.$confirm('确定取消任务？', '提示', {});
      await OrderApi.cancelOrder({ orderId });

      this.listData();
      this.$Utils.notifySuccess('取消成功！');
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      // 获取列表数据
      let cond = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        taskType: this.filters.taskType,
        taskState: this.filters.taskState,
        orderNo: this.filters.orderNo,
      };
      TaskApi.listTask(cond)
        .then((res) => {
          this.dataList = res.data.list;
          this.page.pageTotal = res.data.total;
          this.listLoading = false;
          setTimeout(() => {
            this.$refs.tableList.doLayout();
          });
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },
  },
};
</script>
<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
<style lang="scss" scoped>
.time-select {
  &::v-deep .el-input--suffix {
    .el-input__inner {
      padding-right: 50px;
    }
    &::after {
      position: absolute;
      content: '分钟';
      top: 50%;
      transform: translateY(-50%);
      right: 27px;
      color: #e5e5e5;
      // padding: 70px;
    }
  }
}
</style>
